#include "header/pyramid_room2_objects.h"
#include "header/pyramid_mosaic.h"
#include "header/pyramid_stargate.h"
#include "header/pyramid_torch.h"

OSG_USING_NAMESPACE

NodePtr createRoom2Objects()
{
	Matrix m;
		
		
#ifdef MOSAICS
	/*
	// *********** Mosaic 1 ***********
	NodePtr Mosaic1 = getNamedNode(scene,"mosaic1");
	NodePtr Mosaic2 = getNamedNode(scene,"mosaic2");
	NodePtr Mosaic3 = getNamedNode(scene,"mosaic3");
	NodePtr Mosaic4 = getNamedNode(scene,"mosaic4");
	*/
	
	// Mosaics rule

	NodePtr mosaic1 = createMosaic(1);
	setName(mosaic1,"mosaic1");

	// Transformation
	TransformPtr tmosaic1 = Transform::create();
	beginEditCP(tmosaic1, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setScale(0.6);
		m.setTranslate(0, 20, 60);
		tmosaic1->setMatrix(m);
	endEditCP(tmosaic1, Transform::MatrixFieldMask);

	NodePtr mosaic1Trans = Node::create();
	beginEditCP(mosaic1Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		mosaic1Trans->setCore(tmosaic1);
		mosaic1Trans->addChild(mosaic1);
	endEditCP(mosaic1Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);


	// *********** Mosaic 2 ***********
	NodePtr mosaic2 = createMosaic(2);
	setName(mosaic2,"mosaic2");
	// Transformation
	TransformPtr tmosaic2 = Transform::create();
	beginEditCP(tmosaic2, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setScale(0.6);
		m.setTranslate(0, 20, 180);
		tmosaic2->setMatrix(m);
	endEditCP(tmosaic2, Transform::MatrixFieldMask);
		
	NodePtr mosaic2Trans = Node::create();
	beginEditCP(mosaic2Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		mosaic2Trans->setCore(tmosaic2);
		mosaic2Trans->addChild(mosaic2);
	endEditCP(mosaic2Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	// *********** Mosaic 1 ***********
	NodePtr mosaic3 = createMosaic(3);
	setName(mosaic3,"mosaic3");
	// Transformation
	TransformPtr tmosaic3 = Transform::create();
	beginEditCP(tmosaic3, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(250, 20, 60);
		m.setScale(0.6);
		tmosaic3->setMatrix(m);
	endEditCP(tmosaic3, Transform::MatrixFieldMask);
		
	NodePtr mosaic3Trans = Node::create();
	beginEditCP(mosaic3Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		mosaic3Trans->setCore(tmosaic3);
		mosaic3Trans->addChild(mosaic3);
	endEditCP(mosaic3Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	// *********** Mosaic 4 ***********
	NodePtr mosaic4 = createMosaic(4);
	setName(mosaic4,"mosaic4");
	// Transformation
	TransformPtr tmosaic4 = Transform::create();
	beginEditCP(tmosaic4, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setScale(0.6);
		m.setTranslate(250, 20, 180);
		tmosaic4->setMatrix(m);
	endEditCP(tmosaic4, Transform::MatrixFieldMask);
		
	NodePtr mosaic4Trans = Node::create();
	beginEditCP(mosaic4Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		mosaic4Trans->setCore(tmosaic4);
		mosaic4Trans->addChild(mosaic4);
	endEditCP(mosaic4Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
#endif
	
	
	//room2Torch1
	NodePtr room2Torch1 = createTorch();
	//Transformation
	TransformPtr troom2Torch1 = Transform::create();
	beginEditCP(troom2Torch1, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(4.5,42,150);
		troom2Torch1->setMatrix(m);
	endEditCP(troom2Torch1, Transform::MatrixFieldMask);
	
	NodePtr room2Torch1Trans = Node::create();
	beginEditCP(room2Torch1Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		room2Torch1Trans->setCore(troom2Torch1);
		room2Torch1Trans->addChild(room2Torch1);
	endEditCP(room2Torch1Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);

	//room2Torch2
	NodePtr room2Torch2 = createTorch();
	//Transformation
	TransformPtr troom2Torch2 = Transform::create();
	beginEditCP(troom2Torch2, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setTranslate(245.5,42,150);
		m.setRotate(Quaternion(Vec3f(0,1,0), +3.14159));
		troom2Torch2->setMatrix(m);
	endEditCP(troom2Torch2, Transform::MatrixFieldMask);
	
	NodePtr room2Torch2Trans = Node::create();
	beginEditCP(room2Torch2Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		room2Torch2Trans->setCore(troom2Torch2);
		room2Torch2Trans->addChild(room2Torch2);
	endEditCP(room2Torch2Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);	
	
	// TARDIS model

	cout << "--> parsing external object: tardis.wrl" << endl;
	NodePtr external_tardis = SceneFileHandler::the().read("external_objects/tardis.wrl");
	
	// Transformation
	TransformPtr ttardis = Transform::create();
	beginEditCP(ttardis, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setScale(12);
		m.setTranslate(30, 0, 270);
		ttardis->setMatrix(m);
	endEditCP(ttardis, Transform::MatrixFieldMask);

	TransformPtr rtardis = Transform::create();
	beginEditCP(rtardis, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setRotate(Quaternion(Vec3f(0,1,0), M_PI/2));
		rtardis->setMatrix(m);
	endEditCP(rtardis, Transform::MatrixFieldMask);

	NodePtr external_tardis_Rot = Node::create();
	beginEditCP(external_tardis_Rot, Node::CoreFieldMask | Node::ChildrenFieldMask);
		external_tardis_Rot->setCore(rtardis);
		external_tardis_Rot->addChild(external_tardis);
	endEditCP(external_tardis_Rot, Node::CoreFieldMask | Node::ChildrenFieldMask);

	NodePtr external_tardis_Trans = Node::create();
	beginEditCP(external_tardis_Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		external_tardis_Trans->setCore(ttardis);
		external_tardis_Trans->addChild(external_tardis_Rot);
	endEditCP(external_tardis_Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);


	// sand

	cout << "--> parsing external object: sand.wrl" << endl;
	NodePtr external_sand = SceneFileHandler::the().read("external_objects/sand.wrl");
	
	// Transformation
	TransformPtr tsand = Transform::create();
	beginEditCP(tsand, Transform::MatrixFieldMask);
		m.setIdentity();
		m.setScale(0.15);
		m.setTranslate(10, 0, 10);
		tsand->setMatrix(m);
	endEditCP(tsand, Transform::MatrixFieldMask);

	TransformPtr rsand = Transform::create();
	beginEditCP(rsand, Transform::MatrixFieldMask);
		m.setIdentity();
		//m.setRotate(Quaternion(Vec3f(0,1,0), M_PI/2));
		rsand->setMatrix(m);
	endEditCP(rsand, Transform::MatrixFieldMask);

	NodePtr external_sand_Rot = Node::create();
	beginEditCP(external_sand_Rot, Node::CoreFieldMask | Node::ChildrenFieldMask);
		external_sand_Rot->setCore(rsand);
		external_sand_Rot->addChild(external_sand);
	endEditCP(external_sand_Rot, Node::CoreFieldMask | Node::ChildrenFieldMask);

	NodePtr external_sand_Trans = Node::create();
	beginEditCP(external_sand_Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		external_sand_Trans->setCore(tsand);
		external_sand_Trans->addChild(external_sand_Rot);
	endEditCP(external_sand_Trans, Node::CoreFieldMask | Node::ChildrenFieldMask);

	// Stargate model

	NodePtr stargate = createStargateModel();

	// Erstellen eines Knotens
	NodePtr room2Objects = Node::create();
	beginEditCP(room2Objects, Node::CoreFieldMask);
	{
		room2Objects->setCore(Group::create());		
#ifdef MOSAICS
		room2Objects->addChild(mosaic1Trans);
		room2Objects->addChild(mosaic2Trans);
		room2Objects->addChild(mosaic3Trans);
		room2Objects->addChild(mosaic4Trans);
#endif	
		room2Objects->addChild(external_tardis_Trans);
		room2Objects->addChild(stargate);

		room2Objects->addChild(external_sand_Trans);

		room2Objects->addChild(room2Torch1Trans);
		room2Objects->addChild(room2Torch2Trans);
		
	}
	endEditCP  (room2Objects, Node::CoreFieldMask);
	
	
	return room2Objects;
}
